package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.ChainCallback;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctCertCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctStatsCmd;
import com.winchain.chainbill.bill.biz.receive.cmd.BillHldrCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.trade.cmd.TranCallbackCmd;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctRevCallbackPrntSubscriber extends OnErrorNotImplementedSubscriber<BillTranEvent<CbDct>> {
	@Autowired
	private BillOptCmd<CbDct> billOptCmd;
	@Autowired
	private BillMoveCmd<CbDct> billMoveCmd;
	@Autowired
	private BillDctCmd billDctCmd;
	@Autowired
	private BillDctCertCmd billDctCertCmd;
	@Autowired
	private TranCallbackCmd<Bill<CbDct>> tranCallbackCmd;
	@Autowired
	private BillDctStatsCmd billDctStatsCmd;
	@Autowired
	private BillHldrCmd<CbDct> billHldrCmd;

	public void onApplicationEvent(BillTranEvent<CbDct> t) {
		Bill<CbDct> bill = t.getBill();

		List<Bill<CbDct>> child = (List<Bill<CbDct>>) t.getChildren();
		AcctTxRec tran = t.getTran();

		CbOpt opt = new CbOpt();
		opt.setCurState("S030");
		if (child.size() == 0) {
			opt.setOptCode("EX11");
		} else {
			opt.setOptCode("EX13");
		}
		opt.setRemark("����������������������������");

		CallbackContext<Bill<CbDct>, AcctTxRec> context = new CallbackContext();
		context.setSource(bill);
		context.setTarget(tran);
		context.setValue("Operation", opt);
		context.setValue("children", child);

		ChainCallback<Bill<CbDct>, AcctTxRec> ccb = new ChainCallback();
		ccb.add(this.billOptCmd);
		if (((CbDct) bill.getSource()).getDctId() != null) {
			context.setValue("OwnerId", ((CbDct) bill.getSource()).getFinId());
			context.setValue("OwnerName", ((CbDct) bill.getSource()).getFinNm());
			ccb.add(this.billHldrCmd);
			ccb.add(this.billMoveCmd);

			ccb.add(this.billDctCmd);
			ccb.add(this.billDctCertCmd);
			ccb.add(this.tranCallbackCmd);
		} else {
			ccb.add(this.billMoveCmd);
		}
		ccb.add(this.billDctStatsCmd);

		ccb.callback(context);
	}
}
